import utility
from socket import *
import array
import binascii
import struct
import time
import random

def send_data(sock, data):
    length = len(data)
    send_len = 0
    try:
        while send_len < length:
            send_len += sock.send(data[send_len:])
    except Exception, ex:
        print ex
        return
    return send_len

def recv_data(sock, length):
    try:
        data_package = ""
        left_length = length
        while 1:
            data = sock.recv(left_length)
            if not data:
                globalDef.logger.warn('client close connection!')
                return data
            data_package += data
            left_length -= data.__len__()
            if left_length<=0:
                return data_package
    except Exception, ex:
        print ex


def encodeBody(cid):
    try:
        offset = 0
        arr = array.array('B', '\x00'*409600)

        arr_offset_list = [arr, offset]

        # version
        utility.net_oreder_putInt(arr_offset_list, 100)
        # sign
        utility.net_oreder_putInt(arr_offset_list, 99887766)
        #
        utility.net_oreder_putInt(arr_offset_list, 0)
        #
        utility.net_oreder_putInt(arr_offset_list, 0)

        # bodylen
        utility.net_oreder_putInt(arr_offset_list, 40)

	# reserved
        utility.net_oreder_putInt(arr_offset_list, 0)
        utility.net_oreder_putInt(arr_offset_list, 0)
        utility.net_oreder_putInt(arr_offset_list, 0)
        utility.net_oreder_putInt(arr_offset_list, 0)

        # command id
        utility.net_oreder_putInt(arr_offset_list, 103)

	# cid
        #cid_str = '9988776655443322110099887766554433221100'
        #cid = binascii.a2b_hex(cid_str)
	#cid = '12345678901234567890'
	#cid = '09876543211234567890'
        utility.net_oreder_putLongString(arr_offset_list,cid)

        # gicd
        gcid_str = '9988776655443322110099887766554433221100'
        gcid = binascii.a2b_hex(gcid_str)
        utility.net_oreder_putLongString(arr_offset_list,gcid)

	#filesize
        filesize = 1234
        utility.net_oreder_put64(arr_offset_list, filesize)

        #count
        count = 16
        utility.net_oreder_putInt(arr_offset_list, count)

        #peerid        
        #for i in xrange(count):
	utility.net_oreder_putLongString(arr_offset_list, '0000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, '1000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, '2000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, '3000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, '4000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, '5000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, '6000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, '7000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, '8000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, '9000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, 'A000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, 'B000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, 'C000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, 'D000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, 'E000000000000000')
	utility.net_oreder_putLongString(arr_offset_list, 'F000000000000000')


    ##
    ##    utility.putShort(arr_offset_list, 5)
    ##    utility.putByte(arr_offset_list, 0)
    ##
    ##    utility.putShort(arr_offset_list, 6)
    ##    utility.putByte(arr_offset_list, 50)


        bodylen = arr_offset_list[1]
        #bodylen = htonl(bodylen)

        struct.pack_into('!I', arr, 16, bodylen)

	#print str(arr[:bodylen])
    except Exception, ex:
        print ex
    
    return arr[:arr_offset_list[1]].tostring()

def parseReturnData(head):
    try:
        offset = 0
        arrayhead = array.array('B', head)
        arr_offset_list = [arrayhead, offset]

        #encoder <<protocol_ver<<sequence
        #<<peerid<<product_flag<<release_id<<buddy<<commandid
        pro_ver = utility.getInt(arr_offset_list)
        sequence =utility.getInt(arr_offset_list)
        bodylen = utility.getInt(arr_offset_list)
        return bodylen
    except Exception, ex:
        print ex

def random_cid():
    cid = ''
    for i in range(20):
        b = random.randint(0,0xFF)
        cid += struct.pack('B',b)

    return cid

def start():
    ip = "58.61.39.244"
    port = 9596
    iptuple = (ip, port)

    sock = socket(AF_INET, SOCK_STREAM)
    sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)

    try:
        sock.connect(iptuple)
    except Exception, ex:
        print ex

    #for line in sys.stdin:
    while True:
        cid = random_cid()
        #cid_str = line.strip()
        #print 'read cid: %s' % cid_str
        #cid = binascii.a2b_hex(cid_str)
        noEncryptBody = encodeBody(cid)

        send_len = send_data(sock, noEncryptBody)
        if not send_len:
            print "resend ... "
            send_data(sock, noEncryptBody)

    raw_input('press any key')


if __name__ == '__main__':
    for i in range(1):
        try:
            start()
        except Exception, ex:
            print ex
